﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using fideeraSoft.Utils;
using fideeraSoft.Handlers;
using System.Drawing;
using fideeraSoft.Entities;


namespace fideeraSoft.Paneles
{
    class ProveedorPanelBaja:PanelGeneral
    {
        private Button btBuscarProvPorCuil;
        private TextBox tbBuscarProvCuil;
        private TableLayoutPanel tlpBuscarProvCuil;        
        private MetodosGenerales metGral;
        private DataGridView dgvProveedorBaja;
        private Button btEliminar;
        private Button btVolver;
        
        private Button btBuscarProvRazon;
        private TableLayoutPanel tlpBuscarProvRazon;
        private TextBox tbBuscarProvRazon;
        private int posicionDataGrid;
        private TableLayoutPanel tlpBotones;
        private int posicionTablaBuscarProvRazon;
        private int posicionTablaBotones;
        private static int initialPointVertical = 25;
        

        public ProveedorPanelBaja(Handler handler)
        {
            this.formatearPanelGeneral(handler);
   
            metGral=new MetodosGenerales();

            this.InitializeComponent();

            this.formatearTableTitulo("BAJA DE PROVEEDOR");
            formatearTablaBuscarProvCuil();
            formatearTablaBuscarProvRazon();
            formatearDataGrid();

            formatearTablaBotones();


            this.Controls.Add(TlpTitulo);
            this.Controls.Add(tlpBuscarProvCuil);
            this.Controls.Add(tlpBuscarProvRazon);
            this.Controls.Add(dgvProveedorBaja);
            this.Controls.Add(tlpBotones);
            
        }

        private void formatearTablaBuscarProvCuil()
        {
            tlpBuscarProvCuil.Controls.Add(btBuscarProvPorCuil);
            tlpBuscarProvCuil.Controls.Add(tbBuscarProvCuil);
                        
            tlpBuscarProvCuil.Location = new Point(MetodosGenerales.calcularCenterWidth(this.Size.Width, this.tlpBuscarProvCuil.Size.Width), this.InitialRealPointVertical);
          
        }

        private void formatearTablaBuscarProvRazon()
        {
            tlpBuscarProvRazon.Controls.Add(btBuscarProvRazon);
            tlpBuscarProvRazon.Controls.Add(tbBuscarProvRazon);


            posicionTablaBuscarProvRazon = this.InitialRealPointVertical+tlpBuscarProvCuil.Size.Height;
                                    
            tlpBuscarProvRazon.Location = new Point(MetodosGenerales.calcularCenterWidth(this.Size.Width, this.tlpBuscarProvRazon.Size.Width), posicionTablaBuscarProvRazon);

        }

        private void formatearDataGrid()
        {

            posicionDataGrid = posicionTablaBuscarProvRazon + 60;

            dgvProveedorBaja.AutoGenerateColumns = true;
            dgvProveedorBaja.Size = new Size(this.Size.Width - 100, 100);
            dgvProveedorBaja.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.Fill);
            dgvProveedorBaja.AutoResizeRows(DataGridViewAutoSizeRowsMode.AllHeaders);
            dgvProveedorBaja.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;            
            dgvProveedorBaja.BorderStyle = BorderStyle.Fixed3D;            
            
            dgvProveedorBaja.Location = new Point(MetodosGenerales.calcularCenterWidth(this.Size.Width, dgvProveedorBaja.Size.Width), posicionDataGrid);
        }
        private void formatearTablaBotones()
        {
            tlpBotones.Controls.Add(btVolver);
            tlpBotones.Controls.Add(btEliminar);

            posicionTablaBotones = posicionDataGrid + dgvProveedorBaja.Size.Height + 40;
            tlpBotones.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
            
            //tlpBotones.Size = new Size(400, tbBuscarProvCuil.Size.Height + 9);
            tlpBotones.Location = new Point(MetodosGenerales.calcularCenterWidth(this.Size.Width, this.tlpBotones.Size.Width), posicionTablaBotones);

        }

        private void InitializeComponent()
        {
            this.btBuscarProvPorCuil = new System.Windows.Forms.Button();
            this.tbBuscarProvCuil = new System.Windows.Forms.TextBox();
            this.tlpBuscarProvCuil = new System.Windows.Forms.TableLayoutPanel();
            this.dgvProveedorBaja = new System.Windows.Forms.DataGridView();
            this.btEliminar = new System.Windows.Forms.Button();
            this.btVolver = new System.Windows.Forms.Button();
            this.btBuscarProvRazon = new System.Windows.Forms.Button();
            this.tlpBuscarProvRazon = new System.Windows.Forms.TableLayoutPanel();
            this.tbBuscarProvRazon = new System.Windows.Forms.TextBox();
            this.tlpBotones = new System.Windows.Forms.TableLayoutPanel();
            ((System.ComponentModel.ISupportInitialize)(this.dgvProveedorBaja)).BeginInit();
            this.SuspendLayout();
            // 
            // btBuscarProvPorCuil
            // 
            this.btBuscarProvPorCuil.BackColor = System.Drawing.Color.AliceBlue;
            this.btBuscarProvPorCuil.Dock = System.Windows.Forms.DockStyle.Fill;
            this.btBuscarProvPorCuil.Font = new System.Drawing.Font("Bodoni MT", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btBuscarProvPorCuil.Location = new System.Drawing.Point(0, 0);
            this.btBuscarProvPorCuil.Name = "btBuscarProvPorCuil";
            this.btBuscarProvPorCuil.Size = new System.Drawing.Size(75, 23);
            this.btBuscarProvPorCuil.TabIndex = 0;
            this.btBuscarProvPorCuil.Text = "Buscar Proveedor (Cuil)";
            this.btBuscarProvPorCuil.UseVisualStyleBackColor = false;
            this.btBuscarProvPorCuil.Click += new System.EventHandler(this.btBuscarProvPorCuil_Click);
            // 
            // tbBuscarProvCuil
            // 
            this.tbBuscarProvCuil.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tbBuscarProvCuil.Location = new System.Drawing.Point(0, 0);
            this.tbBuscarProvCuil.MaxLength = 11;
            this.tbBuscarProvCuil.Name = "tbBuscarProvCuil";
            this.tbBuscarProvCuil.Size = new System.Drawing.Size(140, 30);
            this.tbBuscarProvCuil.TabIndex = 0;
            // 
            // tlpBuscarProvCuil
            // 
            this.tlpBuscarProvCuil.BackColor = System.Drawing.Color.LightCyan;
            this.tlpBuscarProvCuil.CellBorderStyle = System.Windows.Forms.TableLayoutPanelCellBorderStyle.Single;
            this.tlpBuscarProvCuil.ColumnCount = 2;
            this.tlpBuscarProvCuil.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBuscarProvCuil.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBuscarProvCuil.Location = new System.Drawing.Point(0, 0);
            this.tlpBuscarProvCuil.Name = "tlpBuscarProvCuil";
            this.tlpBuscarProvCuil.RowCount = 1;
            this.tlpBuscarProvCuil.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBuscarProvCuil.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBuscarProvCuil.Size = new System.Drawing.Size(500, 40);
            this.tlpBuscarProvCuil.TabIndex = 0;
            // 
            // dgvProveedorBaja
            // 
            this.dgvProveedorBaja.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dgvProveedorBaja.Location = new System.Drawing.Point(0, 0);
            this.dgvProveedorBaja.Name = "dgvProveedorBaja";
            this.dgvProveedorBaja.ReadOnly = true;
            this.dgvProveedorBaja.Size = new System.Drawing.Size(240, 150);
            this.dgvProveedorBaja.TabIndex = 0;
            // 
            // btEliminar
            // 
            this.btEliminar.BackColor = System.Drawing.Color.AliceBlue;
            this.btEliminar.Font = new System.Drawing.Font("Bodoni MT", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btEliminar.Location = new System.Drawing.Point(0, 0);
            this.btEliminar.Margin = new System.Windows.Forms.Padding(12, 3, 3, 3);
            this.btEliminar.Name = "btEliminar";
            this.btEliminar.Size = new System.Drawing.Size(75, 40);
            this.btEliminar.TabIndex = 0;
            this.btEliminar.Text = "Eliminar";
            this.btEliminar.UseVisualStyleBackColor = false;
            this.btEliminar.Click += new System.EventHandler(this.btEliminar_Click);
            // 
            // btVolver
            // 
            this.btVolver.BackColor = System.Drawing.Color.AliceBlue;
            this.btVolver.Font = new System.Drawing.Font("Bodoni MT", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btVolver.Location = new System.Drawing.Point(0, 0);
            this.btVolver.Margin = new System.Windows.Forms.Padding(12, 3, 3, 3);
            this.btVolver.Name = "btVolver";
            this.btVolver.Size = new System.Drawing.Size(75, 40);
            this.btVolver.TabIndex = 0;
            this.btVolver.Text = "Volver";
            this.btVolver.UseVisualStyleBackColor = false;
            this.btVolver.Click += new System.EventHandler(this.btVolver_Click);
            // 
            // btBuscarProvRazon
            // 
            this.btBuscarProvRazon.BackColor = System.Drawing.Color.AliceBlue;
            this.btBuscarProvRazon.Dock = System.Windows.Forms.DockStyle.Fill;
            this.btBuscarProvRazon.Font = new System.Drawing.Font("Bodoni MT", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.btBuscarProvRazon.Location = new System.Drawing.Point(0, 0);
            this.btBuscarProvRazon.Name = "btBuscarProvRazon";
            this.btBuscarProvRazon.Size = new System.Drawing.Size(75, 23);
            this.btBuscarProvRazon.TabIndex = 0;
            this.btBuscarProvRazon.Text = "Buscar Proveedor (Razón Social)";
            this.btBuscarProvRazon.UseVisualStyleBackColor = false;
            this.btBuscarProvRazon.Click += new System.EventHandler(this.btBuscarProvRazon_Click);
            // 
            // tlpBuscarProvRazon
            // 
            this.tlpBuscarProvRazon.BackColor = System.Drawing.Color.LightCyan;
            this.tlpBuscarProvRazon.CellBorderStyle = System.Windows.Forms.TableLayoutPanelCellBorderStyle.Single;
            this.tlpBuscarProvRazon.ColumnCount = 2;
            this.tlpBuscarProvRazon.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBuscarProvRazon.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBuscarProvRazon.Location = new System.Drawing.Point(0, 0);
            this.tlpBuscarProvRazon.Name = "tlpBuscarProvRazon";
            this.tlpBuscarProvRazon.RowCount = 1;
            this.tlpBuscarProvRazon.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBuscarProvRazon.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBuscarProvRazon.Size = new System.Drawing.Size(500, 40);
            this.tlpBuscarProvRazon.TabIndex = 0;
            // 
            // tbBuscarProvRazon
            // 
            this.tbBuscarProvRazon.Font = new System.Drawing.Font("Bodoni MT", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tbBuscarProvRazon.Location = new System.Drawing.Point(0, 0);
            this.tbBuscarProvRazon.MaxLength = 50;
            this.tbBuscarProvRazon.Name = "tbBuscarProvRazon";
            this.tbBuscarProvRazon.Size = new System.Drawing.Size(220, 30);
            this.tbBuscarProvRazon.TabIndex = 0;
            // 
            // tlpBotones
            // 
            this.tlpBotones.BackColor = System.Drawing.Color.LightCyan;
            this.tlpBotones.ColumnCount = 2;
            this.tlpBotones.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBotones.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBotones.Location = new System.Drawing.Point(0, 0);
            this.tlpBotones.Name = "tlpBotones";
            this.tlpBotones.RowCount = 1;
            this.tlpBotones.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBotones.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tlpBotones.Size = new System.Drawing.Size(200, 46);
            this.tlpBotones.TabIndex = 0;
            ((System.ComponentModel.ISupportInitialize)(this.dgvProveedorBaja)).EndInit();
            this.ResumeLayout(false);

        }

        private void btBuscarProvPorCuil_Click(object sender, EventArgs e)
        {
            dgvProveedorBaja.ClearSelection();
            if (this.dgvProveedorBaja.DataSource != null)
            {
                this.dgvProveedorBaja.DataSource = null;
            }
            else
            {
                //dgvProveedorBaja.Rows.Remove(dgvProveedorBaja.Rows[e.RowIndex]);
                //dgvProveedorBaja.Rows.Remove(dgvProveedorBaja.Rows[0]);
                this.dgvProveedorBaja.Rows.Clear();
            }
            dgvProveedorBaja.Refresh();

            if (!string.IsNullOrEmpty(tbBuscarProvCuil.Text))
            {
                if (tbBuscarProvCuil.Text.Length == 11)
                {
                    try
                    {
                        Proveedor prov=handler.buscarProveedorPorCuil(tbBuscarProvCuil.Text);
                        if(prov!=null)
                        {
                            List<Proveedor> lstProveedor=new List<Proveedor>();
                            lstProveedor.Add(prov);
                            dgvProveedorBaja.DataSource = lstProveedor;
                         //   MessageBox.Show("CARGA DE PROVEEDOR ENCONTRADO", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); //DEBUG
                            dgvProveedorBaja.EndEdit();
                            dgvProveedorBaja.Refresh();                            

                        }else
                        {
                            MessageBox.Show("NO SE ENCONTRO EL PROVEEDOR", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        }
                    }
                    catch (MiException exc)
                    {
                        //MessageBox.Show(exc.Message);     //DEBUG
                        CLogger.Append("[btBuscarProvPorCuil_Click] Exception: " + exc, CLogger.ERROR);      //DEBUG
                    }
                    
                }
                else
                {
                    MessageBox.Show("NO SE INGRESO NINGÚN NUMERO DE CUIL \nÓ LA CANTIDAD DE DIGITOS ES INCORRECTA", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    
                }
                tbBuscarProvCuil.Clear();
            }
            else
            {
                MessageBox.Show("NO SE INGRESO NINGÚN NUMERO DE CUIL \nÓ LA CANTIDAD DE DIGITOS ES INCORRECTA", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        private void btBuscarProvRazon_Click(object sender, EventArgs e)
        {
            dgvProveedorBaja.ClearSelection();
              if (this.dgvProveedorBaja.DataSource != null)
            {
                this.dgvProveedorBaja.DataSource = null;
            }
            else
            {
                //dgvProveedorBaja.Rows.Remove(dgvProveedorBaja.Rows[e.RowIndex]);
                //dgvProveedorBaja.Rows.Remove(dgvProveedorBaja.Rows[0]);
                this.dgvProveedorBaja.Rows.Clear();
            }
            dgvProveedorBaja.Refresh();

            if (!string.IsNullOrEmpty(tbBuscarProvRazon.Text))
            {                
                try
                {
                    Proveedor prov=handler.buscarProveedorPorRazonSocial(tbBuscarProvRazon.Text);
                    if(prov!=null)
                    {
                        List<Proveedor> lstProveedor=new List<Proveedor>();
                        lstProveedor.Add(prov);

                        dgvProveedorBaja.DataSource = lstProveedor;
                        //   MessageBox.Show("CARGA DE PROVEEDOR ENCONTRADO", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); //DEBUG
                        dgvProveedorBaja.EndEdit();
                        dgvProveedorBaja.Refresh();                            

                    }else
                    {
                        MessageBox.Show("NO SE ENCONTRO EL PROVEEDOR", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                }
                catch (MiException exc)
                {
                    //MessageBox.Show(exc.Message);     //DEBUG
                    CLogger.Append("[btBuscarProvPorCuil_Click] Exception: " + exc, CLogger.ERROR);      //DEBUG
                }
                tbBuscarProvRazon.Clear();
                
            }
            else
            {
                MessageBox.Show("NO SE INGRESO NINGÚN NUMERO DE CUIL \nÓ LA CANTIDAD DE DIGITOS ES INCORRECTA", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        
        }

        private void btEliminar_Click(object sender, EventArgs e)
        {

            try
            {
                if ((dgvProveedorBaja != null) && (dgvProveedorBaja.Rows.Count != 0) )
                {
                    int indx = dgvProveedorBaja.CurrentCell.RowIndex;

                    string strDataId = dgvProveedorBaja[0, indx].Value.ToString();
                    string strDataName = dgvProveedorBaja[1, indx].Value.ToString();

                    DialogResult res = MessageBox.Show("Seguro quiere eliminar al proveedor '" + strDataName + "' - ID: " + strDataId, "INFO", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                    if (res == DialogResult.Yes)
                    {
                        this.handler.eliminarProveedorPorId(Convert.ToInt32(strDataId));
                        MessageBox.Show("Proveedor eliminado correctamente", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        tbBuscarProvCuil.Clear();
                        tbBuscarProvRazon.Clear();

                        if (this.dgvProveedorBaja.DataSource != null)
                        {
                            this.dgvProveedorBaja.DataSource = null;
                        }
                        else
                        {
                            //dgvProveedorBaja.Rows.Remove(dgvProveedorBaja.Rows[e.RowIndex]);
                            //dgvProveedorBaja.Rows.Remove(dgvProveedorBaja.Rows[0]);
                            this.dgvProveedorBaja.Rows.Clear();
                        }
                        dgvProveedorBaja.Refresh();
                    }
                    else
                    {

                    }
                }
                else
                {
                    MessageBox.Show("No hay ningún proveedor en la lista", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
            catch (MiException exc)
            {
                MessageBox.Show("No se pudo eliminar proveedor", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                CLogger.Append("[btEliminar_Click] Exception: " + exc, CLogger.ERROR);      //DEBUG
            }
        }
        private void btVolver_Click(object sender, EventArgs e)
        {
            this.handler.backToPrincipal();
        }
    }
}
